package com.lycos.ludrien.client;

import java.util.ArrayList;
import java.util.Collections;

public class Resultat {

	private class StructResultat implements Comparable<StructResultat>{

		
		public String nomDoc;
		public String cheminPara;
		public double score;
		@Override
		
		public int compareTo(StructResultat o) {
			if (o.score > this.score) return -1;
			else if (o.score < this.score) return 1;
			else return 0;
		}
		
	}
	
	public static final double seuil = 1;
	
	private ArrayList<StructResultat> resultats;
	
	public Resultat (){
		resultats = new ArrayList<Resultat.StructResultat>();
	}
	
	public void ajouterResultat(String p_nomDoc, String p_nomPara, double p_score){
		
		StructResultat sr = new StructResultat();
		sr.cheminPara = p_nomPara;
		sr.nomDoc = p_nomDoc;
		sr.score = p_score;
		
		resultats.add(sr);
		
		Collections.sort(resultats, Collections.reverseOrder());
		
		//System.out.println(p_nomDoc + " , " + p_nomPara + " , " + p_score);
	}
	
	
	public void combinerResultat(Resultat p_resultat,double p_coeff_poid){
		for(StructResultat sr: p_resultat.resultats){
			if (this.dejaPresent(sr.nomDoc, sr.cheminPara))
				this.modifierScore(sr.nomDoc, sr.cheminPara, sr.score * p_coeff_poid);
			else
				this.ajouterResultat(sr.nomDoc, sr.cheminPara, sr.score);
				
		}
	}
	
	public int size(){
		return resultats.size();
	}
	
	public Boolean dejaPresent(String p_nomDoc, String p_nomPara){
		for(StructResultat sr: resultats){
			if (sr.nomDoc.equals(p_nomDoc) && sr.cheminPara.equals(p_nomPara)) return true;
		}
		return false;
	}
	
	public void modifierScore(String p_nomDoc, String p_nomPara, double p_score){
		for(StructResultat sr: resultats){
			if (sr.nomDoc.equals(p_nomDoc) && sr.cheminPara.equals(p_nomPara)) sr.score += p_score;
		}
	}
	
	public String getNomDoc(int index){
		return resultats.get(index).nomDoc;
	}
	
	public String getCheminPara(int index){
		return resultats.get(index).cheminPara;
	}
	
	public Boolean estPertinant(int index){
		return (resultats.get(index).score > seuil);
	}
	
	public int nbPertinant(){
		int nb = 0;
		for(StructResultat sr: resultats){
			if (sr.score > seuil) nb++;
		}
		return nb;
	}
	
	
	public void afficher(int nb){
		int i = 0;
		for(StructResultat sr: resultats){
			i++;
			if (sr.score > seuil)
			System.out.println(sr.nomDoc + " , " + sr.cheminPara + " , " + sr.score);
			if (i > nb) break;
		}
	}
	
	
}
